/*
 *  Copyright 2015 GoIn Inc. All rights reserved.
 */

package com.sharefang.aproject.net;



import com.sharefang.aproject.bean.SelfBean;
import com.sharefang.aproject.bean.UserBean;
import com.sharefang.aproject.constant.CacheKey;
import com.sharefang.aproject.utils.CacheHelper;
import com.sharefang.aproject.utils.CheckUtil;
import com.sharefang.aproject.utils.PackageInfoTool;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * A helper of retrofit2 for creating restful service easier.
 * <p/>
 * Created by Eric on 15/10/18.
 */
public class RetrofitClient implements NetApi, CacheKey {

    private static final RetrofitClient instance = new RetrofitClient();

    public static RetrofitClient getInstance() {
        return instance;
    }

    private Retrofit retrofit;

    public RetrofitClient() {
        createRetrofit();
    }

    private void createRetrofit() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = getHeader(chain);
                        return chain.proceed(request);
                    }
                })
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }


    public <T> T create(Class<?> clazz) {
        return (T) retrofit.create(clazz);
    }

    /**
     * 根据是否已经登录，添加不同的Header信息
     *
     * @param chain
     * @return
     */
    private Request getHeader(Interceptor.Chain chain) {
        boolean isLogined = CheckUtil.isLogined();
        Request request = null;
        if (isLogined) {
            request = chain.request()
                    .newBuilder()
                    .addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6")
                    .addHeader("X-Requested-With", PackageInfoTool.getOsVersion() + ";" + PackageInfoTool.getVersion())
                    .addHeader("x-access-token", ((SelfBean) CacheHelper.getAsObject(SELF_BEAN)).getAccessToken())
                    .addHeader("Connection", "keep-alive")
                    .build();
        } else {
            request = chain.request()
                    .newBuilder()
                    .addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6")
                    .addHeader("X-Requested-With", PackageInfoTool.getOsVersion() + ";" + PackageInfoTool.getVersion())
                    .addHeader("Connection", "keep-alive")
                    .build();
        }
        return request;
    }
}


